import 'dart:typed_data';

import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';

import 'package:hzy_project_template/commpontents/commpontents_index.dart';
import 'package:hzy_project_template/controllers/user_c.dart';
import 'package:hzy_project_template/models/image_normal_model.dart';
import 'package:hzy_project_template/utils/utils_index.dart';
import 'package:hzy_project_template/base_ui_config/base_ui_config_index.dart';
import '../../config/config_index.dart';
import '../../utils/image_editor_tool.dart';

class NormalHeaderEditImageV extends CommonGetXV<NormalHeaderEditImageC> {
  NormalHeaderEditImageV({Key? key}) : super(key: key);
  @override
  NormalHeaderEditImageC get controller => Get.put(NormalHeaderEditImageC());

  @override
  Widget createBody({
    required BuildContext context,
    BoxConstraints? constraints,
  }) {
    Widget body = Column(
      children: [
        createBigImageWidget(),
        Gaps.vGap20,
        createBottomBgWidget(),
      ],
    );
    return body;
  }

  /// 创建顶部大图
  createBigImageWidget() {
    Widget body = createImageBuilder(
      imageNormalModel: controller.imageNormalModel,
      isGesture: true,
      width: SizeMacro.screenWidth - 20.w,
      boxFit: BoxFit.contain,
      mode: ExtendedImageMode.editor,
      enableLoadState: true,
      colorBlendMode: BlendMode.clear,
      extendedImageEditorKey: controller.editorKey,
      editorConfig: EditorConfig(
        maxScale: 8.0,
        cropRectPadding: const EdgeInsets.all(20.0),
        hitTestSize: 20.0,
        cornerColor: HzyNormalColorS.colffffff,
        cropAspectRatio: CropAspectRatios.ratio1_1,
        initCropRectType: InitCropRectType.imageRect,
        editorMaskColorHandler: (yorn, colr) {
          return Colors.white.withOpacity(0);
        },
      ),
    );
    body = Center(
      child: body,
    );
    body = Expanded(child: body);
    return body;
  }

  /// 创建底部视图
  createBottomBgWidget() {
    Widget body = createEditBtnWidget(
      otherWidget: createReset(),
      onTap: (index) {
        controller.tapEdit(
          editType: index,
        );
      },
    );

    body = Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        body,
        Gaps.vGap10,
        HzyNormalTools.configLineSpace(
          color: HzyNormalColorS.colefedf3.withOpacity(
            0.1,
          ),
        ),
        Gaps.vGap10,
        createBottomNavBarWidget(
          title: NoteLauncIdConfig.edit.tr,
          tapSure: () {
            controller.tapSure();
          },
        ),
      ],
    );

    body = createBoxShadowBottomNavBarWidget(
      child: body,
    );
    return body;
  }

  /// 创建 还原按钮
  Widget createReset() {
    Widget body = HzyBtnWidget(
      btnLayoutType: BtnLayoutType.topImg,
      imageWidget: const Icon(
        Icons.replay,
        color: HzyNormalColorS.colffffff,
        size: 24,
      ),
      height: null,
      titleWidget: Text(
        NoteLauncIdConfig.reDuction.tr,
        style: HzyTextStyle.fontMedium(
          size: 14,
          color: HzyNormalColorS.colffffff,
        ),
      ),
      tapCall: () {
        controller.tapReDuction();
      },
    );

    body = Row(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        body,
      ],
    );

    body = Expanded(
      child: body,
    );
    return body;
  }

  @override
  IconData? createLeadIcon() {
    return Icons.close;
  }

  @override
  Color? createLeadingIconColor() {
    return HzyNormalColorS.colffffff;
  }
}

class NormalHeaderEditImageC extends CommonGetXController {
  // --------- 属性 --------- //
  late ImageNormalModel imageNormalModel;
  GlobalKey<ExtendedImageEditorState> editorKey =
      GlobalKey<ExtendedImageEditorState>();

  // --------- 生命周期  --------- //
  @override
  configDefault() {
    scallBackGroundColor = normalBlackColor;
    navBackgroudColor = normalBlackColor;
    isShowAppBar = false;
    imageNormalModel = currentGetArguments();
  }
  // --------- 数据处理  --------- //

  // --------- 触发事件  --------- //

  /// 点击编辑
  tapEdit({
    required int editType,
  }) {
    switch (editType) {
      case 0:
        editorKey.currentState?.rotate();
        break;
      case 1:
        editorKey.currentState?.flip();
        break;
      default:
    }
  }

  /// 点击还原
  tapReDuction() {
    editorKey.currentState?.reset();
  }

  /// 点击确认
  tapSure() async {
    Uint8List? uint8list = await ImageEditorTool.cropImageDataWithBty(
      state: editorKey.currentState!,
    );
    if (UserC.isLogin) {
      await UserC.current.configUserImageModel(
        imageList: [
          ImageNormalModel(byte: uint8list),
        ],
      );
    }

    showToastMsg(NoteLauncIdConfig.modify.tr, onTap: () {
      currentGoback(
        info: {
          "info": ImageNormalModel(byte: uint8list),
        },
      );
    });
  }
}
